﻿using KinonekoSoftware.CodariaDB.Documents.TemplateContents;
using KinonekoSoftware.CodariaDB.Documents.Theories.Vocabulary;
using KinonekoSoftware.CodariaDB.Languages;
using KinonekoSoftware.UI;

    using KinonekoSoftware.Foundation.UI.Media;

namespace KinonekoSoftware.Studio.Pages.Instances.Templated
{
    public class NewColoredVocabularyViewModel : ObjectDialog<ColoredVocabulary>
    {
        private const string Key_Vocabularies = "t";


        public static Task<Result<ColoredVocabulary>> New(TheoryEngine_Vocabulary tot)
        {
            var context = new DialogContext();
            context.Set(Key_Vocabularies, tot);
            return Dialog<ColoredVocabulary>(new NewColoredVocabularyViewModel(), context);
        }


        public static Task<Result<ColoredVocabulary>> Edit(TheoryEngine_Vocabulary tot, ColoredVocabulary target)
        {
            var context = new DialogContext();
            context.Set(Key_Vocabularies, tot);
            context.Set(ContextBase.Key_Value, target);
            return Dialog<ColoredVocabulary>(new NewColoredVocabularyViewModel(), context);
        }

        private string            _content;
        private Color             _color;
        private ColoredVocabulary _target;
        private string            _suffix;
        private string            _prefix;
        private Vocabulary        _currentVocabulary;
        private SolidColorBrush   _brush;

        public NewColoredVocabularyViewModel()
        {
            Color        = MediaFactory.ToColor(BrushKey.ForegroundLevel1);
            Vocabularies = new ObservableCollection<Vocabulary>();
        }

        private void UpdateContent()
        {
            if (CurrentVocabulary is null)
            {
                return;
            }

            Content = $"{_prefix} {CurrentVocabulary.Name} {_suffix}";
            Color   = MediaFactory.ToColor(CurrentVocabulary.BorderBrush);
        }

        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(Key_Vocabularies))
            {
                var terms = context.GetObject<TheoryEngine_Vocabulary>(Key_Vocabularies);
                Vocabularies.AddMany(terms.Collection, true);
            }


            if (context.Has(ContextBase.Key_Value))
            {
                _target = context.GetObject<ColoredVocabulary>(ContextBase.Key_Value);
                Content = _target.Content;
                Color   = MediaFactory.ToColor(_target.Color);
                return true;
            }

            return false;
        }

        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(
                      DialogRule.Create<NewColoredVocabularyViewModel>(
                                                                           CodariaSR.Text_Parameter_EmptyName,
                                                                           x => !string.IsNullOrEmpty(x.Content)));
        }

        protected override ColoredVocabulary OnFinish(bool editing)
        {

            if (editing)
            {
                _target.Content = Content;
                _target.Color   = Color.ToString();
                return _target;
            }

            return new ColoredVocabulary
            {
                Content = Content,
                Color   = Color.ToString(),
            };
        }

        /// <summary>
        /// 获取或设置 <see cref="Color"/> 属性。
        /// </summary>
        public Color Color
        {
            get => _color;
            set
            {
                SetValue(ref _color, value);
                Brush = new SolidColorBrush(value);
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="Brush"/> 属性。
        /// </summary>
        public SolidColorBrush Brush
        {
            get => _brush;
            private set => SetValue(ref _brush, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="Content"/> 属性。
        /// </summary>
        public string Content
        {
            get => _content;
            set => TryFinishAndSetValue(ref _content, value);
        }


        /// <summary>
        /// 获取或设置 <see cref="CurrentVocabulary"/> 属性。
        /// </summary>
        public Vocabulary CurrentVocabulary
        {
            get => _currentVocabulary;
            set
            {
                TryFinishAndSetValue(ref _currentVocabulary, value);
                UpdateContent();
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="Prefix"/> 属性。
        /// </summary>
        public string Prefix
        {
            get => _prefix;
            set
            {
                TryFinishAndSetValue(ref _prefix, value);
                UpdateContent();
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="Suffix"/> 属性。
        /// </summary>
        public string Suffix
        {
            get => _suffix;
            set
            {
                TryFinishAndSetValue(ref _suffix, value);
                UpdateContent();
            }
        }

        public ObservableCollection<Vocabulary> Vocabularies { get; }
    }
}